package com.server.db;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 数据存储
 */
public class DbLock {

	public static void lock(Object obj,ReentrantLock[] locks){
		ReentrantLock lock = getLock(obj,locks);
		lock.lock();
	}
	
	public static void unlock(Object obj,ReentrantLock[] locks){
		ReentrantLock lock = getLock(obj,locks);
		lock.unlock();
	}
	
	private static ReentrantLock getLock(Object k,ReentrantLock[] locks) {
		int h = 0;
		h ^= k.hashCode();
		h += (h << 15) ^ 0xffffcd7d;
		h ^= (h >>> 10);
		h += (h << 3);
		h ^= (h >>> 6);
		h += (h << 2) + (h << 14);
		int index = (locks.length - 1) & (h ^ (h >>> 16));
		return locks[index];
	}
	
	// users
	public static final ReentrantLock[] users_locks = new ReentrantLock[16];
	// 兵营队列
	public static final ReentrantLock[] bydl_locks = new ReentrantLock[16];
	// 建筑队列
	public static final ReentrantLock[] jzdl_locks = new ReentrantLock[16];
	// 服务器
	public static final ReentrantLock[] fwq_locks = new ReentrantLock[16];
	// 玩家
	public static final ReentrantLock[] wj_locks = new ReentrantLock[16];
	// 用户
	public static final ReentrantLock[] yh_locks = new ReentrantLock[16];
	// 用户建筑
	public static final ReentrantLock[] yhjz_locks = new ReentrantLock[16];
	// 用户映射
	public static final ReentrantLock[] yhys_locks = new ReentrantLock[16];
	// 用户类型
	public static final ReentrantLock[] yhlx_locks = new ReentrantLock[16];
	// 用户背包
	public static final ReentrantLock[] yhbb_locks = new ReentrantLock[16];
	// 用户英雄
	public static final ReentrantLock[] yhyx_locks = new ReentrantLock[16];
	// 用户装饰
	public static final ReentrantLock[] yhzs_locks = new ReentrantLock[16];
	// 用户邮件
	public static final ReentrantLock[] yhyj_locks = new ReentrantLock[16];
	// 用户陷阱
	public static final ReentrantLock[] yhxj_locks = new ReentrantLock[16];
	// 用户障碍
	public static final ReentrantLock[] yhza_locks = new ReentrantLock[16];
	// 聊天
	public static final ReentrantLock[] lt_locks = new ReentrantLock[16];
	// 聊天类型
	public static final ReentrantLock[] ltlx_locks = new ReentrantLock[16];
	// 联盟
	public static final ReentrantLock[] lm_locks = new ReentrantLock[16];
	// 英雄技能
	public static final ReentrantLock[] yxjn_locks = new ReentrantLock[16];
	// 英雄状态
	public static final ReentrantLock[] yxzt_locks = new ReentrantLock[16];
	// 邮件类型
	public static final ReentrantLock[] yjlx_locks = new ReentrantLock[16];
	// 邮件附件
	public static final ReentrantLock[] yjfj_locks = new ReentrantLock[16];
	// 附件类型
	public static final ReentrantLock[] fjlx_locks = new ReentrantLock[16];
	// 障碍队列
	public static final ReentrantLock[] zadl_locks = new ReentrantLock[16];
	
	
	static{
		// users
		for (int i = 0; i < users_locks.length; i++)users_locks[i] = new ReentrantLock();
		// 兵营队列
		for (int i = 0; i < bydl_locks.length; i++)bydl_locks[i] = new ReentrantLock();
		// 建筑队列
		for (int i = 0; i < jzdl_locks.length; i++)jzdl_locks[i] = new ReentrantLock();
		// 服务器
		for (int i = 0; i < fwq_locks.length; i++)fwq_locks[i] = new ReentrantLock();
		// 玩家
		for (int i = 0; i < wj_locks.length; i++)wj_locks[i] = new ReentrantLock();
		// 用户
		for (int i = 0; i < yh_locks.length; i++)yh_locks[i] = new ReentrantLock();
		// 用户建筑
		for (int i = 0; i < yhjz_locks.length; i++)yhjz_locks[i] = new ReentrantLock();
		// 用户映射
		for (int i = 0; i < yhys_locks.length; i++)yhys_locks[i] = new ReentrantLock();
		// 用户类型
		for (int i = 0; i < yhlx_locks.length; i++)yhlx_locks[i] = new ReentrantLock();
		// 用户背包
		for (int i = 0; i < yhbb_locks.length; i++)yhbb_locks[i] = new ReentrantLock();
		// 用户英雄
		for (int i = 0; i < yhyx_locks.length; i++)yhyx_locks[i] = new ReentrantLock();
		// 用户装饰
		for (int i = 0; i < yhzs_locks.length; i++)yhzs_locks[i] = new ReentrantLock();
		// 用户邮件
		for (int i = 0; i < yhyj_locks.length; i++)yhyj_locks[i] = new ReentrantLock();
		// 用户陷阱
		for (int i = 0; i < yhxj_locks.length; i++)yhxj_locks[i] = new ReentrantLock();
		// 用户障碍
		for (int i = 0; i < yhza_locks.length; i++)yhza_locks[i] = new ReentrantLock();
		// 聊天
		for (int i = 0; i < lt_locks.length; i++)lt_locks[i] = new ReentrantLock();
		// 聊天类型
		for (int i = 0; i < ltlx_locks.length; i++)ltlx_locks[i] = new ReentrantLock();
		// 联盟
		for (int i = 0; i < lm_locks.length; i++)lm_locks[i] = new ReentrantLock();
		// 英雄技能
		for (int i = 0; i < yxjn_locks.length; i++)yxjn_locks[i] = new ReentrantLock();
		// 英雄状态
		for (int i = 0; i < yxzt_locks.length; i++)yxzt_locks[i] = new ReentrantLock();
		// 邮件类型
		for (int i = 0; i < yjlx_locks.length; i++)yjlx_locks[i] = new ReentrantLock();
		// 邮件附件
		for (int i = 0; i < yjfj_locks.length; i++)yjfj_locks[i] = new ReentrantLock();
		// 附件类型
		for (int i = 0; i < fjlx_locks.length; i++)fjlx_locks[i] = new ReentrantLock();
		// 障碍队列
		for (int i = 0; i < zadl_locks.length; i++)zadl_locks[i] = new ReentrantLock();
	}
}